﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;

namespace PAS.Helper
{
    public class OptionImportor:AbstractImportor
    {
        protected Hashtable m_PeopleNameSet = new Hashtable();


        public OptionImportor(string excelPah, string sheetName)
            : base(excelPah, sheetName)
        {
            m_KeyColName = OptionInfo.MemoName;
        }


        public void CheckData()
        {
            if (m_SourceData == null)
                InitSourceData();
            if (m_SourceData != null)
            {
                foreach (string key in m_SourceData.Keys)
                {
                    var rowData = ((DataRow) m_SourceData[key]);
                    CheckEachRowDataFormat(key, rowData);
                }
                GetObjects();
                foreach (string key in m_SourceData.Keys)
                {
                    var rowData = ((DataRow) m_SourceData[key]);
                    CheckRowDataByDbObject(key, rowData);
                }
            }
        }

        protected override void CheckEachRowDataFormat(string key, DataRow rowData)
        {
            string strName = Convert.ToString(rowData[OptionInfo.MemoName]);
            string strGroupName = Convert.ToString(rowData[OptionInfo.MemoGroupName]);

            if (strName.Length == 0)
            {
                SetErrMessage(key, "[" + OptionInfo.MemoName + "]不能为空");
            }
            else
            {
                if (strName.Length > 20)
                    SetErrMessage(key, "输入的[" + OptionInfo.MemoName + "]长度不能大于20,当前为" + strName.Length);
            }
            if (strGroupName.Length > 20)
                SetErrMessage(key, "输入的[" + OptionInfo.MemoGroupName + "]长度不能大于20,当前为" + strGroupName.Length);
        }

        protected override void GetObjects()
        {
            var hlp = new DataConnHelper();
            m_PeopleNameSet = hlp.GetHashtableByNames(OptionInfo.DbTableName, OptionInfo.DbName,
                                                      " Order by " + OptionInfo.DbName);
        }

        protected override void CheckRowDataByDbObject(string key, DataRow dr)
        {
            if (!m_ErrDataLocatoins.Contains(key) && dr != null && dr.Table.Columns.Contains(OptionInfo.MemoName))
            {
                string strName = Convert.ToString(dr[OptionInfo.MemoName]);
                if (strName.Length > 0)
                {
                    if (m_PeopleNameSet.Contains(strName))
                        SetErrMessage(key, "输入的" + OptionInfo.MemoName + "[" + strName + "]在系统已经存在,不需要导入");
                }
                if (!m_ErrDataLocatoins.Contains(key))
                    m_RightDataLocatoins.Add(key);
            }
        }

        protected override string CreateInsertSql(DataRow rowData)
        {
            string strName = Convert.ToString(rowData[OptionInfo.MemoName]);
            string strGroupName = Convert.ToString(rowData[OptionInfo.MemoGroupName]);
            return ("Insert into " + OptionInfo.DbTableName + " (" + OptionInfo.DbName + "," +
                    OptionInfo.DbGroupName + ") values('" + strName + "','" + strGroupName + "');");
        }
    }
}
